package models

import (
	"time"
)

// User 用户模型
type User struct {
	ID             int64     `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
	Username       string    `gorm:"column:username;type:varchar(50);uniqueIndex:uk_username;not null" json:"username"`
	Password       string    `gorm:"column:password;type:varchar(100);not null" json:"-"`
	Email          string    `gorm:"column:email;type:varchar(100);uniqueIndex:uk_email;not null" json:"email"`
	Phone          string    `gorm:"column:phone;type:varchar(20)" json:"phone"`
	Name           string    `gorm:"column:name;type:varchar(50)" json:"name"`
	Status         int8      `gorm:"column:status;type:tinyint;not null;default:1;index:idx_status" json:"status"`
	LastLoginTime  time.Time `gorm:"column:last_login_time" json:"last_login_time"`
	LastLoginIP    string    `gorm:"column:last_login_ip;type:varchar(50)" json:"last_login_ip"`
	CreatedAt      time.Time `gorm:"column:created_at;not null;default:CURRENT_TIMESTAMP" json:"created_at"`
	UpdatedAt      time.Time `gorm:"column:updated_at;not null;default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" json:"updated_at"`
	InvitationCode string    `gorm:"column:invitation_code;type:varchar(50)" json:"invitation_code"`
	RoleID         int64     `gorm:"column:role_id;not null;default:1;index:fk_user_role" json:"role_id"`
	IsDeleted      int8      `gorm:"column:is_deleted;not null;default:0" json:"is_deleted"`
}

// TableName 获取表名
func (User) TableName() string {
	return "users"
}

// UserAuthMethod 用户认证方式模型
type UserAuthMethod struct {
	BaseModel
	UserID     int64  `json:"user_id" gorm:"not null;index:idx_user_id"`
	AuthType   string `json:"auth_type" gorm:"type:varchar(20);not null"`
	AuthSecret string `json:"auth_secret" gorm:"type:varchar(100)"`
	AuthConfig string `json:"auth_config" gorm:"type:json"`
	IsEnabled  int8   `json:"is_enabled" gorm:"type:tinyint;default:0;not null;index:idx_is_enabled"`
	Priority   int8   `json:"priority" gorm:"type:tinyint;default:0;not null;index:idx_priority"`
}

// TableName 获取表名
func (UserAuthMethod) TableName() string {
	return "user_auth_methods"
}

// UserLoginRecord 用户登录记录模型
type UserLoginRecord struct {
	ID        int64     `json:"id" gorm:"primaryKey;autoIncrement"`
	UserID    int64     `json:"user_id" gorm:"not null;index:idx_user_id"`
	IP        string    `json:"ip" gorm:"type:varchar(50);not null"`
	LoginTime time.Time `json:"login_time" gorm:"not null;index:idx_login_time"`
	UserAgent string    `json:"user_agent" gorm:"type:text"`
	Location  string    `json:"location" gorm:"type:varchar(255);default:'未知'"`
	IsUnusual int8      `json:"is_unusual" gorm:"type:tinyint;default:0;not null;index:idx_is_unusual"`
	CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
}

// TableName 获取表名
func (UserLoginRecord) TableName() string {
	return "user_login_records"
}

// UserRegisterRequest 用户注册请求
type UserRegisterRequest struct {
	Username         string `json:"username" binding:"required,min=3,max=50"`
	Password         string `json:"password" binding:"required,min=6,max=20"`
	Email            string `json:"email" binding:"required,email"`
	Phone            string `json:"phone" binding:"required,len=11"`
	Name             string `json:"name" binding:"max=50"`
	SecurityQuestion string `json:"security_question" binding:"required"`
	SecurityAnswer   string `json:"security_answer" binding:"required"`
	VerificationCode string `json:"verification_code" binding:"required"`
	InvitationCode   string `json:"invitation_code,omitempty"`
}

// UserRegisterResponse 用户注册响应
type UserRegisterResponse struct {
	Token string   `json:"token"`
	User  UserInfo `json:"user"`
}

// UserInfo 用户信息
type UserInfo struct {
	ID        int64     `json:"id"`
	Username  string    `json:"username"`
	Phone     string    `json:"phone"`
	Email     string    `json:"email"`
	RoleID    int64     `json:"role_id"`
	Status    int       `json:"status"`
	CreatedAt time.Time `json:"created_at"`
}

// UserLoginRequest 用户登录请求
type UserLoginRequest struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
}

// UserLoginResponse 用户登录响应
type UserLoginResponse struct {
	Token string   `json:"token"`
	User  UserInfo `json:"user"`
}

// UserProfileResponse 用户资料响应
type UserProfileResponse struct {
	ID             int64      `json:"id"`
	Username       string     `json:"username"`
	Email          string     `json:"email"`
	Phone          string     `json:"phone"`
	Name           string     `json:"name"`
	Status         int        `json:"status"`
	LastLoginTime  *time.Time `json:"last_login_time"`
	LastLoginIP    string     `json:"last_login_ip"`
	CreatedAt      time.Time  `json:"created_at"`
	UpdatedAt      time.Time  `json:"updated_at"`
	InvitationCode string     `json:"invitation_code"`
	RoleID         int64      `json:"role_id"`
}

// UserInfoResponse 用户信息响应
type UserInfoResponse struct {
	ID               int64      `json:"id"`
	Username         string     `json:"username"`
	Email            string     `json:"email"`
	Phone            string     `json:"phone"`
	Status           int8       `json:"status"`
	LastLoginTime    *time.Time `json:"lastLoginTime"`
	ExchangeLossRate string     `json:"exchangeLossRate"`
}
